package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Building;
import com.woniuxy.oldmanapi.service.BuildingService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BuildingController {

    @Autowired
    BuildingService buildingService;

    @GetMapping("/building/list")
    public ResponseResult<List<Building>> list() {
        ResponseResult<List<Building>> result = new ResponseResult<>();
        try {
            List<Building> lists = buildingService.findBuildings();
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @GetMapping("/building/num")
    public ResponseResult<Integer> num() {
        ResponseResult<Integer> result = new ResponseResult<>();
        try {
            int a = buildingService.findBuildingNum();
            result.setCode(200);
            result.setMsg("查数成功!");
            result.setData(a);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查数失败!");
        }
        return result;
    }

    @PostMapping("/building/add")
    public ResponseResult<Void> add(@RequestBody Building building) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            buildingService.add(building);
            result.setCode(200);
            result.setMsg("添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @PutMapping("/building/modify")
    public ResponseResult<Void> modify(@RequestBody Building building) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            buildingService.modify(building);
            result.setCode(200);
            result.setMsg("修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @DeleteMapping("/building/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            buildingService.delete(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result;
    }

    @GetMapping("/building/one/{id}")
    public ResponseResult<Building> one(@PathVariable("id") int id) {
        ResponseResult<Building> result = new ResponseResult<>();
        try {
            Building building = buildingService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(building);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
}
